package weeny.demo;

import static java.awt.geom.AffineTransform.getRotateInstance;
import static weeny.pre.renderer.DataShapeRenderer.SHAPE_FIELD;

import java.awt.Shape;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import prefuse.Constants;
import prefuse.render.ShapeRenderer;
import prefuse.visual.VisualItem;
import weeny.pre.display.MarkerDisplay;
import weeny.pre.renderer.DataShapeRenderer;
import weeny.pre.util.IntColor;
import weeny.pre.util.Pre;
import wordle.layout.Wordle;
import wordle.layout.WordleAlgorithm;

class ShapeWordle extends Wordle {
	public final VisualItem item;
	ShapeWordle(VisualItem item) {
		this.item = item;
	}
	public Shape getShape() {
		return (Shape) item.get(SHAPE_FIELD);
	}
}

public class WordleDemo2 extends MarkerDisplay{
	private static final Random RAND = new Random(12345L);
	private static final String GROUP = SHAPE_FIELD+"_GROUP";
	private static final Point2D ZERO = new Point2D.Double();
	
	private void init() {
		getVisualization().addFocusGroup(GROUP);
		marker.addRenderer(GROUP, new DataShapeRenderer());
		marker.addVisualNodeField(SHAPE_FIELD, Shape.class);
		ShapeRenderer shapeRen = new ShapeRenderer();
		
		List<ShapeWordle> list = new ArrayList<ShapeWordle>();
		for(int i = 0; i<100; i++){
			double size = RAND.nextDouble()*10;
			int shape = RAND.nextInt(Constants.SHAPE_COUNT);
			int color = IntColor.CATEGORY_5[RAND.nextInt(5)]; 
			VisualItem vi = drawShape(ZERO, size, shape, color);
			Shape raw = shapeRen.getShape(vi);
			double angle = (vi.hashCode()%100)/7.1;
			Shape glyph = getRotateInstance(angle).createTransformedShape(raw);
			vi.set(SHAPE_FIELD, glyph);
			getVisualization().getFocusGroup(GROUP).addTuple(vi);
			list.add(new ShapeWordle(vi));
		}
		WordleAlgorithm alg = new WordleAlgorithm(800,600);
		alg.setHorizontalExpansion(1);
		alg.setVerticalExpansion(1);
		alg.setHorizontalMinSize(4);
		alg.setVerticalMinSize(4);
		alg.calc(list);
		for (ShapeWordle shapeWordle : list) {
			Pre.setPos(shapeWordle.item, shapeWordle.getLocation());
		}
	}
	private WordleDemo2() {
		setSize(800, 600);
		showItemControlTabInTree();
		showIODisplayTab();
		init();
	}
	public static void main(String[] args) {
		new WordleDemo2().showFrame("Wordle Demo 2");
	}
}